﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace GameJam
{
    public class MoveCollideDetector : MonoBehaviour
    {
        private List<Collider2D> touchEntityList = new List<Collider2D>();
        private Entity entity;
        //private DIR lastUnableDir = DIR.LEFT;

        void Awake()
        {
            entity = transform.parent.GetComponent<Entity>();
        }

        // Use this for initialization
        void Start()
        {

        }

        //// Update is called once per frame
        void Update()
        {

            int num = touchEntityList.Count;
            for (int i = num - 1; i >= 0; i--)
            {
                if (touchEntityList[i] == null)
                {
                    touchEntityList.Remove(touchEntityList[i]);
                }
            }
        }


        void OnTriggerEnter2D(Collider2D other)
        {
            if (!this.enabled) return;
            if (other.CompareTag("move_collider") || other.CompareTag("wall"))   //
            {
                touchEntityList.Add(other);
            }

        }

        private bool isHadFly = false;
        void OnTriggerStay2D(Collider2D other)
        {
            if (!this.enabled) return;
            #region 生物与碰撞物
            if (other.gameObject.CompareTag("move_collider") || other.gameObject.CompareTag("wall"))
            {
                Vector3 cur_center = GetComponent<Collider2D>().bounds.center;
                Vector3 o_center = other.bounds.center;
                Vector3 o_bounds = other.bounds.extents;

                float offsetX = Mathf.Min(o_bounds.x * 0.93f, 0.3f);
                Vector3 o_bounds_offset = new Vector2(o_bounds.x - offsetX, o_bounds.y);
                Vector2 tan_angle_offX = new Vector2(Mathf.Atan(o_bounds.y / o_bounds_offset.x), Mathf.Atan(o_bounds.y / -o_bounds_offset.x));
                Vector2 tan_angle_offY = new Vector2(Mathf.Atan(o_bounds_offset.y / o_bounds.x), Mathf.Atan(o_bounds_offset.y / -o_bounds.x));
                float angle = 0;
                if ((cur_center.x - o_center.x) == 0)
                {
                    if ((cur_center.y - o_center.y) > 0)
                    {
                        angle = 90;
                    }
                    else
                    {
                        angle = -90;
                    }
                }
                else
                {
                    angle = Mathf.Atan((cur_center.y - o_center.y) / (cur_center.x - o_center.x));
                }
                DIR touchDir = DIR.LEFT;
                if (cur_center.x > o_center.x)
                {
                    if (angle > tan_angle_offY.x)
                    {
                        touchDir = DIR.DOWN;
                        entity.SendMessage("unableMoveDir", DIR.DOWN);
                    }
                    else if (angle < tan_angle_offY.y)
                    {
                        touchDir = DIR.UP;
                        entity.SendMessage("unableMoveDir", DIR.UP);
                    }
                    else
                    {
                        touchDir = DIR.LEFT;
                        entity.SendMessage("unableMoveDir", DIR.LEFT);
                    }

                    if (angle <= tan_angle_offX.x && angle >= tan_angle_offX.y)
                    {
                        touchDir = DIR.LEFT;
                        entity.SendMessage("unableMoveDir", DIR.LEFT);
                    }

                }
                else if (cur_center.x < o_center.x)
                {
                    if (angle < tan_angle_offY.y)
                    {
                        touchDir = DIR.DOWN;
                        entity.SendMessage("unableMoveDir", DIR.DOWN);
                    }
                    else if (angle > tan_angle_offY.x)
                    {
                        touchDir = DIR.UP;
                        entity.SendMessage("unableMoveDir", DIR.UP);
                    }
                    else
                    {
                        touchDir = DIR.RIGHT;
                        entity.SendMessage("unableMoveDir", DIR.RIGHT);
                    }

                    if (angle <= tan_angle_offX.x && angle >= tan_angle_offX.y)
                    {
                        touchDir = DIR.RIGHT;
                        entity.SendMessage("unableMoveDir", DIR.RIGHT);
                    }

                }
                else//垂直
                {
                    if (cur_center.y > o_center.y)
                    {
                        touchDir = DIR.DOWN;
                        entity.SendMessage("unableMoveDir", DIR.DOWN);
                    }
                    else
                    {
                        touchDir = DIR.UP;
                        entity.SendMessage("unableMoveDir", DIR.UP);
                    }
                }
                if (isResetPos)
                {
                    if (other.gameObject.CompareTag("move_collider"))
                    {
                        isResetPos = false;
                    }
                    Vector3 pos = entity.transform.position;

                    if (other.gameObject.CompareTag("move_collider") || (touchDir == DIR.LEFT || touchDir == DIR.RIGHT))
                    {
                        if (cur_center.x > o_center.x && cur_center.x < o_center.x + o_bounds.x)
                        {
                            //pos.x = o_center.x + o_bounds.x + entity.BodySize.x / 4;
                            entity.transform.position = pos;
                        }
                        else if (cur_center.x < o_center.x && cur_center.x > o_center.x - o_bounds.x)
                        {
                            //pos.x = o_center.x - o_bounds.x - entity.BodySize.x / 4;
                            entity.transform.position = pos;
                        }
                    }
                }
                if (isFocusReset && other.gameObject.CompareTag("wall"))
                {
                    isFocusReset = false;
                    cur_center = startPos;
                    Vector3 pos = entity.transform.position;
                    if ((touchDir == DIR.LEFT || touchDir == DIR.RIGHT))
                    {
                        if (cur_center.x > o_center.x && cur_center.x < o_center.x + o_bounds.x)
                        {
                            //pos.x = o_center.x + o_bounds.x + entity.BodySize.x / 2;
                            entity.transform.position = pos;
                            //Debug.Log("aaa");
                        }
                        else if (cur_center.x < o_center.x && cur_center.x > o_center.x - o_bounds.x)
                        {
                            //pos.x = o_center.x - o_bounds.x - entity.BodySize.x / 2;
                            entity.transform.position = pos;
                            //Debug.Log("bbb");
                        }
                    }
                    else if ((touchDir == DIR.UP || touchDir == DIR.DOWN))
                    {
                        if (cur_center.y > o_center.y && cur_center.y < o_center.y + o_bounds.y)
                        {
                            //pos.y = o_center.y + o_bounds.y + entity.BodySize.z / 2;
                            entity.transform.position = pos;
                            //Debug.Log("ccc");
                        }
                        else if (cur_center.y < o_center.y && cur_center.y > o_center.y - o_bounds.y)
                        {
                            //pos.y = o_center.y - o_bounds.y - entity.BodySize.z / 2;
                            entity.transform.position = pos;
                            //Debug.Log("ddd");
                        }
                    }
                    //Debug.Log(pos);
                }
            }
            #endregion

        }

        private bool IsCanResetPos(Vector3 self_pos, Vector3 other_pos, Vector3 other_halfSize)
        {
            Vector3 other_min = other_pos - other_halfSize;
            Vector3 other_max = other_pos + other_halfSize;
            if (self_pos.x > other_min.x && self_pos.x < other_max.x && self_pos.y > other_min.y && self_pos.y < other_max.y)
            {
                return true;
            }
            return false;
        }

        void OnTriggerExit2D(Collider2D other)
        {
            if (!this.enabled) return;
            if (other.gameObject.CompareTag("move_collider") || other.gameObject.CompareTag("wall"))
            {
                touchEntityList.Remove(other);
                if (entity != null) entity.SendMessage("needResetDir");
            }
        }

        private bool isResetPos = false;
        private bool isFocusReset = false;
        private Vector3 startPos;
        public void ResetPos(bool isReset)
        {
            isResetPos = isReset;
        }

        private void FocusResetPos(Vector3 start_pos)
        {
            isFocusReset = true;
            startPos = start_pos;
        }
    }

}



